L2-003 月饼

题目 L2-003 月饼

image-3928a83f

思路分析

image-b299c6eb

代码实现

#include<bits/stdc++.h>

using namespace std;

#define endl '\n'

using ll = long long;

using ull = unsigned long long;

using PII = pair<int,int>;

using Pll = pair<ll,ll>;

int dx[4]={-1,0,1,0},dy[4]={0,1,0,-1};

const int inf = 0x3f3f3f3f;

using PDD = pair<double,double>;

const int N=1010;

double have[N];

double price[N];

priority_queue<PDD> pie;

int main()

{

	ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);

	int n,need;cin>>n>>need;

	for(int i=0;i<n;i++){

		cin >> have[i];

	}

	for(int i=0;i<n;i++){

		double tmp;cin>>tmp;

		price[i]=tmp*1.0/have[i];

	}

//	for(int i=0;i<n;i++)	cout<<have[i]<<" "<<price[i]<<endl;

	for(int i=0;i<n;i++)	pie.push({price[i],have[i]});

	double sum=0;

	while(!pie.empty() && need>0){

		double cur_price = pie.top().first;

		double cur_have = pie.top().second;

		if(cur_have<=need){

			sum+=cur_have * cur_price;

			need-=cur_have;

		}else if(cur_have>need){

			sum+=need*cur_price;

			need=0;

		}

		pie.pop();

	}

	printf("%.2f",sum);

	return 0;

 }

同类题型

视频讲解


⬅️ L2-002 链表去重 🏠 00-天梯赛 ➡️ L2-004 这是二叉搜索树吗?